package cn.yudao.ymcc.web.controller;

import cn.yudao.ymcc.dto.CrumbVo;
import cn.yudao.ymcc.service.ICourseTypeService;
import cn.yudao.ymcc.domain.CourseType;
import cn.yudao.ymcc.query.CourseTypeQuery;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.result.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseType")
public class CourseTypeController {

    @Autowired
    public ICourseTypeService courseTypeService;


    //courseType/crumbs/
    @RequestMapping(value = "/crumbs/{courseTypeId}",method = RequestMethod.GET)
    public JSONResult crumbs(@PathVariable("courseTypeId")Long courseTypeId){
        List<CrumbVo> crumbs =  courseTypeService.crumbs(courseTypeId);
        return JSONResult.success(crumbs);
    }


    /**
    * @Title: get
    * @Description: 获取树状结构
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/7 17:47
    * @Parameters:
    * @Return
    */
    @RequestMapping(value = "/treeData",method = RequestMethod.GET)
    public JSONResult treeData(){
        return JSONResult.success(courseTypeService.treeData());
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CourseType courseType){
        if(courseType.getId()!=null){
            courseTypeService.updateById(courseType);
        }else{
            courseTypeService.insert(courseType);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseTypeService.deleteById(id);
        return JSONResult.success();
    }
    /**
     * 获取对象

   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseTypeService.selectById(id));
    }



    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseTypeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseTypeQuery query){
        Page<CourseType> page = new Page<CourseType>(query.getPage(),query.getRows());
        Wrapper<CourseType> Wrapper = new EntityWrapper<>();
        Wrapper.like("name",query.getKeyword());
        page = courseTypeService.selectPage(page,Wrapper);
        return JSONResult.success(new PageList<CourseType>(page.getTotal(),page.getRecords()));
    }
}
